//
//  RHLocalBleDeviceCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/25.
//

#import "RHLocalBleDeviceCell.h"
#import "CBLEDataMnger.h"

@interface RHLocalBleDeviceCell()
@property (nonatomic ,strong) QMUILabel *devNameLab;

@property (nonatomic ,strong) QMUIButton *connectBtn;

@property (nonatomic ,strong) QMUILabel *typeContentLab;

@property (nonatomic ,strong) QMUILabel *macContentLab;

@property (nonatomic ,strong) QMUILabel *timeContentLab;

@end
@implementation RHLocalBleDeviceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    
    return self;
}

- (void)setDataInfo:(CDataPheralInfo *)dataInfo{
    
    _dataInfo = dataInfo;
    
    NSNumber *RSSI = _dataInfo.RSSI;
   
    self.devNameLab.text = dataInfo.devName;
    self.macContentLab.text = dataInfo.peripheral.identifier.UUIDString;
    self.typeContentLab.text = dataInfo.deviceType;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dataInfo.connectTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    self.timeContentLab.text = [formatter stringFromDate:date];
   
    
    if (dataInfo.adverMacAddr) {
        self.macContentLab.text = dataInfo.adverMacAddr;
    }
}

- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    bgView.layer.cornerRadius = 8;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    kWeakSelf;
    _devNameLab = [[QMUILabel alloc] init];
    [_devNameLab setText:@"--"];
    [_devNameLab setTextAlignment:NSTextAlignmentCenter];
    [_devNameLab setFont:kBOLDSYSTEMFONT(16)];
    [_devNameLab setTextColor:kRGB(63, 68, 67)];
    [bgView addSubview:_devNameLab];
    
    [_devNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(20);
    }];
    
    _connectBtn = [[QMUIButton alloc]init];
    [_connectBtn setTitle:@"连接".rh_localized forState:UIControlStateNormal];
    [_connectBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _connectBtn.titleLabel.font = kSYSTEMFONT(13);
    [_connectBtn setBackgroundColor:CMainColorGreen];
    [_connectBtn setCornerRadius:8];
    [bgView addSubview:_connectBtn];
    [_connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 35));
        make.centerY.equalTo(weakSelf.devNameLab);
        make.right.mas_offset(-15);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kRGB(235, 237, 240);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.top.equalTo(weakSelf.devNameLab.mas_bottom).offset(20);
        make.height.offset(1);
    }];
    
    
    QMUILabel *typeTitleLab = [[QMUILabel alloc] init];
    [typeTitleLab setText:[NSString stringWithFormat:@"%@:",@"类型".rh_localized]];
    [typeTitleLab setTextAlignment:NSTextAlignmentCenter];
    [typeTitleLab setFont:kSYSTEMFONT(13)];
    [typeTitleLab setTextColor:kColorRGBA(63, 68, 67, 0.8)];
    [bgView addSubview:typeTitleLab];
    
    [typeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(line.mas_bottom).offset(15);
    }];
    
    _typeContentLab = [[QMUILabel alloc] init];
    [_typeContentLab setText:@"--"];
    [_typeContentLab setTextAlignment:NSTextAlignmentCenter];
    [_typeContentLab setFont:kSYSTEMFONT(12)];
    [_typeContentLab setTextColor:kColorRGBA(63, 68, 67, 1)];
    [bgView addSubview:_typeContentLab];
    
    [_typeContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(typeTitleLab);
    }];
    
    QMUILabel *macTitleLab = [[QMUILabel alloc] init];
    [macTitleLab setText:[NSString stringWithFormat:@"%@:",@"MAC".rh_localized]];
    [macTitleLab setTextAlignment:NSTextAlignmentCenter];
    [macTitleLab setFont:kSYSTEMFONT(13)];
    [macTitleLab setTextColor:kColorRGBA(63, 68, 67, 0.8)];
    [bgView addSubview:macTitleLab];
    
    [macTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeTitleLab);
        make.top.equalTo(typeTitleLab.mas_bottom).offset(15);
      
    }];
    
    _macContentLab = [[QMUILabel alloc] init];
    [_macContentLab setText:@"--"];
    [_macContentLab setTextAlignment:NSTextAlignmentCenter];
    [_macContentLab setFont:kSYSTEMFONT(12)];
    [_macContentLab setTextColor:kColorRGBA(63, 68, 67, 1)];
    [bgView addSubview:_macContentLab];
    
    [_macContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(macTitleLab);
    }];
    
    QMUILabel *timeTitleLab = [[QMUILabel alloc] init];
    [timeTitleLab setText:[NSString stringWithFormat:@"%@:",@"时间".rh_localized]];
    [timeTitleLab setTextAlignment:NSTextAlignmentCenter];
    [timeTitleLab setFont:kSYSTEMFONT(13)];
    [timeTitleLab setTextColor:kColorRGBA(63, 68, 67, 0.8)];
    [bgView addSubview:timeTitleLab];
    
    [timeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(macTitleLab);
        make.top.equalTo(macTitleLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    _timeContentLab = [[QMUILabel alloc] init];
    [_timeContentLab setText:@"--"];
    [_timeContentLab setTextAlignment:NSTextAlignmentCenter];
    [_timeContentLab setFont:kSYSTEMFONT(12)];
    [_timeContentLab setTextColor:kColorRGBA(63, 68, 67, 1)];
    [bgView addSubview:_timeContentLab];
    
    [_timeContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(timeTitleLab);
    }];
    
    [[_connectBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.connectBlock){
            weakSelf.connectBlock();
        }
    }];
}

@end
